//
//  ViewController.m
//  折叠选项卡
//
//  Created by Apple on 2017/7/8.
//  Copyright © 2017年 郭凯. All rights reserved.
//
#define NORMALCELL @"NORMALCELL"
#define SEACTIONCELL @"SECTIONCELL"

#import "ViewController.h"
#import "SectionView.h"
#import "SectionModel.h"
#import "cellModel.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray* seactionDatas;
@end

@implementation ViewController
-(NSMutableArray *)seactionDatas{
    if (!_seactionDatas) {
        
        _seactionDatas = [NSMutableArray array];
        for (NSInteger i= 0; i<100; i++) {
            SectionModel *seaction = [[SectionModel alloc]init];
            seaction.isExpand = NO;
            seaction.title = [NSString stringWithFormat:@"%ld",i];
            
            NSMutableArray *tmp = [NSMutableArray array];
            for (NSInteger j=0; j<30; j++) {
                cellModel *model = [[cellModel alloc]init];
                model.title = [NSString stringWithFormat:@"sdsdssdss__%ld",j];
                [tmp addObject:model];
            }
            seaction.cellArray = tmp;
            [_seactionDatas addObject:seaction];
        }
    }
    return _seactionDatas;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NORMALCELL];
    [self.tableView registerNib:[UINib nibWithNibName:@"SectionView" bundle:nil] forHeaderFooterViewReuseIdentifier:SEACTIONCELL];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.seactionDatas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    SectionModel  *seaction = self.seactionDatas[section];
    return seaction.isExpand ? seaction.cellArray.count : 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NORMALCELL];
    SectionModel *seaction = self.seactionDatas[indexPath.section];
    cellModel *model = seaction.cellArray[indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    SectionView *seactonView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SEACTIONCELL];
    seactonView.contentView.backgroundColor = [UIColor blueColor];
    seactonView.model = self.seactionDatas[section];
      [seactonView setCallBackBlock:^(BOOL isExpand){
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
       }];
    return seactonView;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}
@end
